library(haven)

######################################################################################################
##### Loading data for study 2 (called "study2_replication_data") and save in object "df_study2" #####
##### use the path where you haved saved the data file "study1_replication_data" #####################
##### by uncommenting the line below and provide your path to the file ###############################
######################################################################################################

#df_study2 <- read_dta(".../study2_replication_data.dta")

####################
##### Recoding #####
####################

df_study2$comp_exp.f <-as.factor(df_study2$comp_exp)
df_study2$warmth_exp.f <-as.factor(df_study2$warmth_exp)


############################################################################
##### Figure for main results of study 2 (Figure 4 in main manuscript) #####
############################################################################

library(ggplot2)
library(dotwhisker)
library(stargazer)
library(dplyr)
library(cowplot)


##### Panel A of Figure 4 Competence panel #####

 ##point estimates
coef.matrix <- matrix(c(0.031, 0.043,
				0.018, 0.047,
				0.041, 0.033,
				0.007, 0.018), nr=1)

                         
                          
  ##standard error of matrix
  se.matrix <- matrix(c(0.012, 0.006,
				0.014, 0.007,
				0.016, 0.008,
				0.012, 0.0063), nr=1)
 			

                      
  
  ##variable names
  varnames<- c("Competence")
  
  model_names <- c("Adm. inst.",
                   "Pol. inst.", "Civil servants", "Gov. in Was.")

  submodel_names <- c("Exp. manipulations", "Std. perceptions")
  
 #model_order <- c(1,2,3,4,1,2,3,4)
  
  results_df_study2 <- data.frame(term=rep(varnames[1], times=2),
                           estimate=as.vector(coef.matrix),
                           std.error=as.vector(se.matrix),
                           model=as.factor(rep(model_names, each=2)),
                           submodel=rep(rep(submodel_names, each = 1), times = 4))
  
 
  
mainplot2_competence <- small_multiple(results_df_study2) +
    scale_x_discrete(limits = model_names) +  
    theme_bw() + ylab("Marginal effects on citizen trust") +
    geom_hline(yintercept = 0, colour = "black", linetype = "dashed") +
    theme(axis.text.x  = element_text(size=12),
	    axis.text.y = element_text(size=10),
	    axis.title.y = element_text(size=12),
          legend.position=c(.27, .99), legend.justification=c(1, 1), 
          legend.title = element_text(size=12),
          legend.background = element_rect(color="black"),
          legend.key.size = unit(12, "pt")) +
	    scale_y_continuous(breaks=c(-0.05, 0, 0.05, 0.10),limits=c(-0.05,0.10)) +
          scale_colour_grey(start=0.2, end=0.8, name="Model") +     
          ggtitle("") 

mainplot2_competence


##### Panel B of Figure 4 (Warmth panel) #####

##point estimates
coef.matrix <- matrix(c(0.031, 0.040,
				0.029, 0.056,
				0.051, 0.026,
				0.045, 0.018), nr=1)

                         
                          
  ##standard error of matrix
  se.matrix <- matrix(c(0.012, 0.006,
				0.014, 0.007,
				0.016, 0.0085,
				0.012, 0.0063), nr=1)
 			               
  
  ##variable names
  varnames<- c("Warmth")
  
  model_names <- c("Adm. inst.",
                   "Pol. inst.", "Civil servants", "Gov. in Was.")

  submodel_names <- c("Exp. manipulations", "Std. perceptions")
  
 #model_order <- c(1,2,3,4,1,2,3,4)
  
  results_df_study2 <- data.frame(term=rep(varnames[1], times=2),
                           estimate=as.vector(coef.matrix),
                           std.error=as.vector(se.matrix),
                           model=as.factor(rep(model_names, each=2)),
                           submodel=rep(rep(submodel_names, each = 1), times = 4))
  
 
  
mainplot2_warmth <- small_multiple(results_df_study2) +
    scale_x_discrete(limits = model_names) +  
    theme_bw() + ylab("Marginal effects on citizen trust") +
    geom_hline(yintercept = 0, colour = "black", linetype = "dashed") +
    theme(axis.text.x  = element_text(size=12),
	    axis.text.y = element_text(size=10),
	    axis.title.y = element_text(size=12),
          legend.position=c(.27, .99), legend.justification=c(1, 1), 
          legend.title = element_text(size=12),
          legend.background = element_rect(color="black"),
          legend.key.size = unit(12, "pt")) +
	    scale_y_continuous(breaks=c(-0.05, 0, 0.05, 0.10),limits=c(-0.05,0.10)) +
          scale_colour_grey(start=0.2, end=0.8, name="Model") +     
          ggtitle("") 

mainplot2_warmth

#########################################################################
##### Combining panels and plotting Figure 4 in the main manuscript #####
#########################################################################

fig_4 <- plot_grid(mainplot2_competence, mainplot2_warmth, ncol=1, labels="AUTO")
fig_4

#pdf("plot_study2.pdf")
#fig_4
#dev.off()






